//
//  GoodDetailView.m
//  shanHaiHuiApp
//
//  Created by DS on 2018/3/7.
//  Copyright © 2018年 徐双. All rights reserved.
//

#import "GoodDetailView.h"
#import "DetailTableViewCell.h"
#import "SDCycleScrollView.h"



@interface GoodDetailView()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>

Strong UITableView * tableView;
Strong UIView * headView;
Strong UIView * footView;
Copy void(^selectedHandle)(NSString *str);

//头部视图
Strong UIImageView * picImageView;
Strong UILabel * titleLabel;

Strong NSMutableArray * dataArry;



Strong SDCycleScrollView *sdcView;


@end

//网络
@implementation GoodDetailView (NetWorking)


@end


@implementation GoodDetailView

#pragma mark - Action

//取消按钮
- (void)cancelBtnClick{
    
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}
//减去
- (void)minusBtnClick{
    
    if (_getMinusClick) {
        _getMinusClick(self);
    }
}

//加上
- (void)plusBtnClick{
    
    if (_getPlusClick) {
        _getPlusClick(self);
    }
}

- (void)addShopCarBtnClick{
    if (_getJoinShopCarClick) {
        _getJoinShopCarClick(self);
    }
}

#pragma mark-- UITextFieldDelegate

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    NSString *textStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
    
    [UserModel sharedInstanced].textStr = textStr;
    
    self.goodCount = [textStr integerValue];
    if (_getTextFieldClick) {
        _getTextFieldClick(self);
    }
    
    return YES;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField resignFirstResponder];
}

#pragma mark - init
- (instancetype)initWithFrame:(CGRect)frame selectedHandle:(void (^)(NSString *))selectedHandle{
    if (self = [super initWithFrame:frame]) {
        self.alpha = 0;
        UIButton *backButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
        _selectedHandle = selectedHandle;
        backButton.backgroundColor = [UIColor blackColor];
        backButton.alpha = .6;
        backButton.titleLabel.font = DEF_FontSize_13;
        [backButton addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:backButton];
        [self addSubview:self.tableView];
    }
    return self;
}



- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(UNIT_WIDTH(40), UNIT_HEIGHT(60), SCREEN_WIDTH - UNIT_WIDTH(80), SCREEN_HEIGHT - UNIT_HEIGHT(120)) style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.sectionFooterHeight = .01;
        _tableView.sectionHeaderHeight = .01;
        _tableView.tableFooterView = self.footView;
        _tableView.tableHeaderView = self.headView;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        _tableView.backgroundColor = [UIColor whiteColor];
    }
    return _tableView;
}

- (UIView *)headView{
    if (!_headView) {
        _headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH - UNIT_WIDTH(80), UNIT_HEIGHT(300))];
        
        _headView.backgroundColor = [UIColor whiteColor];
        
        
        UIButton *cancleBtn = [[UIButton alloc] init];
        [cancleBtn setImage:[UIImage imageNamed:@"guanbi"] forState:UIControlStateNormal];
        cancleBtn.contentMode = UIViewContentModeScaleAspectFit;
        [cancleBtn addTarget:self action:@selector(cancelBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.font = DEF_FontSize_12;
        _titleLabel.textColor = getColor(@"323232");
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        _titleLabel.numberOfLines = 0;
        _titleLabel.text = @"八铁,年货干果八铁,年货干果八铁,年货干果八铁,年货干果";
        
        [_headView sd_addSubviews:@[self.sdcView,cancleBtn,self.titleLabel]];
        
        ws(self)
        
        [self.sdcView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(UNIT_HEIGHT(10));
            make.centerX.mas_equalTo(weakself.headView.mas_centerX);
            make.width.mas_equalTo(UNIT_WIDTH(188));
            make.height.mas_equalTo(UNIT_HEIGHT(176));
        }];
        
        [cancleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(UNIT_HEIGHT(10));
            make.right.mas_equalTo(UNIT_HEIGHT(-10));
            make.height.mas_equalTo(UNIT_HEIGHT(30));
            make.width.mas_equalTo(UNIT_WIDTH(30));
        }];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(weakself.sdcView.mas_bottom).offset(UNIT_HEIGHT(30));
            make.left.mas_equalTo(UNIT_WIDTH(20));
            make.width.mas_equalTo(weakself.headView.width - UNIT_WIDTH(40));
        }];
    }
    return _headView;
}

- (UIView *)footView{
    if (!_footView ) {
        _footView = [[UIView alloc] init];
        _footView.frame = CGRectMake(0, _tableView.height - UNIT_HEIGHT(150), SCREEN_WIDTH - UNIT_WIDTH(80),  UNIT_HEIGHT(150));
        _footView.backgroundColor = [UIColor whiteColor];
        
        _pariceLabel = [[UILabel alloc] init];
        _pariceLabel.font = DEF_FontSize_17;
        _pariceLabel.textColor = getColor(@"EC4350");
        _pariceLabel.text = @"￥99.6";

        _perLabel = [[UILabel alloc] init];
        _perLabel.font = DEF_FontSize_12;
        _perLabel.textColor = getColor(@"666666");
        _perLabel.text = @"/￥99.6";

        _minusBtn = [[UIButton alloc] init];
        [_minusBtn setImage:[UIImage imageNamed:@"jian"] forState:UIControlStateNormal];
        [_minusBtn addTarget:self action:@selector(minusBtnClick) forControlEvents:UIControlEventTouchUpInside];

        _plusBtn = [[UIButton alloc] init];
        [_plusBtn setImage:[UIImage imageNamed:@"jia"] forState:UIControlStateNormal];
        [_plusBtn addTarget:self action:@selector(plusBtnClick) forControlEvents:UIControlEventTouchUpInside];

        _textField = [[UITextField alloc] init];
        _textField.font = DEF_FontSize_14;
        _textField.textColor = getColor(@"323232");
        _textField.text = @"2";
        _textField.textAlignment = NSTextAlignmentCenter;
        _textField.layer.borderWidth = 1.0f;
        _textField.layer.borderColor = [UIColor grayColor].CGColor;
        _textField.delegate = self;
        
        _addShopCarBtn = [[UIButton alloc] init];
        _addShopCarBtn.backgroundColor = getColor(@"EC4350");
        [_addShopCarBtn setTitle:@"加入购物车" forState:UIControlStateNormal];
        [_addShopCarBtn setTitleColor:getColor(@"FFFFFF") forState:UIControlStateNormal];
        _addShopCarBtn.titleLabel.font = DEF_FontSize_13;
        [_addShopCarBtn addTarget:self action:@selector(addShopCarBtnClick) forControlEvents:UIControlEventTouchUpInside];

        [_footView sd_addSubviews:@[self.pariceLabel,self.perLabel,self.minusBtn,self.plusBtn,self.textField,self.addShopCarBtn]];
        
        [self layOutFrame];
        
    }
    return _footView;
}

- (SDCycleScrollView *)sdcView{
    if (!_sdcView) {
        //_sdcView = [[SDCycleScrollView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150/WIDTH_5S_SCALE)];
        _sdcView = [[SDCycleScrollView alloc] init];
        _sdcView.autoScrollTimeInterval = 3.0;
        _sdcView.backgroundColor = getColor(@"bfbfbf");
        _sdcView.pageControlDotSize = CGSizeMake(11, 11);
        _sdcView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
        _sdcView.currentPageDotImage = [UIImage imageNamed:@"homepage_banner_point"];
        _sdcView.pageDotImage = [UIImage imageNamed:@"homepage_banner_circle"];
        //__block typeof(self) bSelf = self;
        _sdcView.clickItemOperationBlock = ^(NSInteger currentIndex) {
            //            [[BrowPhotoView alloc]initWithFrame:CGRectMake(0, 0, scrWidth(), scrHeight()) array:self.bannerArr selectIndex:currentIndex];
        };
        
    }
    return _sdcView;
}

- (void)layOutFrame{
    
    ws(self)
    [self.pariceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(UNIT_WIDTH(17));
        make.top.mas_equalTo(UNIT_HEIGHT(21));
        make.height.mas_equalTo(UNIT_HEIGHT(14));
    }];
    
    [self.perLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakself.pariceLabel.mas_right).offset(0);
        make.top.mas_equalTo(weakself.pariceLabel.mas_top);
        make.bottom.mas_equalTo(weakself.pariceLabel.mas_bottom);
    }];
    
    [self.plusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(UNIT_WIDTH(-15));
        make.bottom.mas_equalTo(weakself.pariceLabel.mas_bottom);
        make.width.mas_equalTo(UNIT_WIDTH(30));
        make.height.mas_equalTo(UNIT_HEIGHT(30));
    }];
    
    [self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakself.plusBtn.mas_left).offset(0);
        make.bottom.mas_equalTo(weakself.plusBtn.mas_bottom);
        make.top.mas_equalTo(weakself.plusBtn.mas_top);
        make.width.mas_equalTo(50);
    }];
    
    [self.minusBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(weakself.textField.mas_left).offset(0);
        make.bottom.mas_equalTo(weakself.pariceLabel.mas_bottom);
        make.width.mas_equalTo(UNIT_WIDTH(30));
        make.height.mas_equalTo(UNIT_HEIGHT(30));
    }];
    
    [self.addShopCarBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(UNIT_WIDTH(16));
        make.bottom.mas_equalTo(UNIT_HEIGHT(-17));
        make.width.mas_equalTo(_footView.width - (UNIT_HEIGHT(34)));
        make.height.mas_equalTo(UNIT_HEIGHT(32));
    }];
 
    
}



#pragma mark - <UITableViewDelegate , UITableViewDataSource>

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return self.dataArry.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DetailTableViewCell * cell = [DetailTableViewCell cellForTableView:tableView];
    
    cell.model = self.dataArry[indexPath.row];
    
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UNIT_HEIGHT(44);
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}


//出现
-(void)show{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}

//消失
-(void)dismiss{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0 ;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

- (void)setModel:(NSDictionary *)model{
    _model = model;
    self.titleLabel.text = model[@"fSpmc"];
    self.perLabel.text = [NSString stringWithFormat:@"/￥%@",model[@"fDj2"]];
    self.pariceLabel.text = [NSString stringWithFormat:@"￥%@",model[@"fDj2"]];

    self.dataArry[0][@"content"] = model[@"fCd"];
    self.dataArry[1][@"content"] = model[@"fDw"];
    self.dataArry[2][@"content"] = model[@"fSpgg"];
    [self.tableView reloadData];
}

- (void)setSpkc:(NSNumber *)spkc{
    _spkc = spkc;
    self.dataArry[3][@"content"] = [spkc stringValue];
}

- (void)setImageArr:(NSMutableArray *)imageArr{
    _imageArr = imageArr;
    self.sdcView.imageURLStringsGroup = imageArr;
}
#pragma mark -- 懒加载
- (NSMutableArray *)dataArry{
    if (!_dataArry) {
        _dataArry = @[@{@"title":@"生产厂家",@"content":@""}.mutableCopy,
                      @{@"title":@"单位",@"content":@""}.mutableCopy,
                      @{@"title":@"规格",@"content":@""}.mutableCopy,
                      @{@"title":@"库存",@"content":@""}.mutableCopy,
                      ].mutableCopy;
    }
    return _dataArry;
}



@end
